package itsm.isperp.module.controller.configuration;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilter;
import itsm.isperp.framework.web.request.JqGridFilterRule;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.configuration.ConfCategory;
import itsm.isperp.module.entity.configuration.ConfCategoryProperty;
import itsm.isperp.module.service.configuration.ConfCategoryPropertyService;
import itsm.isperp.module.service.configuration.ConfCategoryService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 配置分类自定义属性管理控制器
 * 
 * @author lizx
 * @date 2013-4-30
 */
@Controller
@RequestMapping("configuration/category/property")
public class ConfCategoryPropertyController extends
		BaseController<ConfCategoryProperty> {

	@Autowired
	ConfCategoryPropertyService confCategoryPropertyService;

	@Autowired
	ConfCategoryService confCategoryService;

	@Override
	public ConfCategoryPropertyService getService() {
		return confCategoryPropertyService;
	}

	@Override
	public String getPageTitle() {
		return "配置分类自定义属性";
	}

	/**
	 * 编辑界面
	 * 
	 * @param id
	 *            唯一标识
	 * @return 编辑视图
	 */
	@Override
	@RequestMapping("edit")
	public ModelAndView edit(String id, HttpServletRequest req) {

		ConfCategory category = null;
		ModelAndView mv = new ModelAndView(this.getModulePath() + "/edit");
		ConfCategoryProperty model = null;
		if (StringUtils.isEmpty(id)) {
			String categoryId = req.getParameter("categoryId");
			if (StringUtils.isEmpty(categoryId)) {
				return null;
			}
			category = confCategoryService.findOne(categoryId);
			model = new ConfCategoryProperty();
			model.setConfCategoryId(categoryId);
		} else {
			model = confCategoryPropertyService.findOne(id);
			category = confCategoryService.findOne(model.getConfCategoryId());
		}
		mv.addObject("pageTitle", this.getPageTitle());
		mv.addObject("model", model);
		mv.addObject("categoryName", category.getName());
		return mv;
	}

	@RequestMapping("getPropertyCountByCategoryPath")
	@ResponseBody
	public long getPropertyCountByCategoryPath(String categoryPath) {

		return this.confCategoryPropertyService
				.getPropertyCountByCategoryPath(categoryPath);
	}

	/**
	 * 根据配置分类Id查询所包含的所有自定义属性
	 * 
	 * @param categoryId
	 *            配置分类Id
	 * @return json数据结果
	 */
	@RequestMapping("listByCategory")
	@ResponseBody
	public List<Map<String, Object>> listByCategory(String categoryId,String tecServiceId,
			JqGridRequest req) {

		if (StringUtils.isEmpty(categoryId)) {
			JqGridFilter filter = req.getFilter();
			if (filter == null) {
				return null;
			}
			List<JqGridFilterRule> list = filter.getRules();
			for (JqGridFilterRule rule : list) {
				if ("categoryId".equals(rule.getField())) {
					categoryId = rule.getData();
					break;
				}
			}
		}
		if (StringUtils.isEmpty(categoryId)) {
			return null;
		}
		List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
		if (StringUtils.isNotEmpty(tecServiceId)) {
			String inStr="";
			String[] temArr=tecServiceId.split(",");
			for(String s:temArr)
			{
				inStr+="'"+s+"',";
			}
			inStr=inStr.substring(0,inStr.length()-1);
			list = this.getService().findAllProperty(
					categoryId,inStr);
			String sql="select count(1) count_c from TEC_SERVICE_RELATE_CONF a left join tec_service_relate_ci_category b on a.tec_relate_ci_category_id=b.id where type='基本'  AND ( (SELECT c.treenode_path FROM conf_category c WHERE c.id='"+categoryId+"') LIKE CONCAT(CONCAT('%',b.ci_category_id),'%')) and a.TEC_SERVICE_ID in ("+inStr+")";
			JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
			Integer countC=jdbcTemplate.queryForObject(sql, Integer.class);
			if(list.size()==0 && countC==0)
			{
				list = this.getService().findAllProperty(
						categoryId);
			}
		}else
		{
			list = this.getService().findAllProperty(
					categoryId); 
		}
		
		return list;
	}
}
